package com.ruoyi.hqfinance.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.hqfinance.domain.HqNetworkBillDetail;
import com.ruoyi.hqfinance.domain.HqNetworkDetailCollect;
import com.ruoyi.hqfinance.domain.vo.HqNetworkBillDetailVo;
import com.ruoyi.hqfinance.mapper.HqNetworkBillDetailMapper;
import com.ruoyi.hqfinance.mapper.HqNetworkDetailCollectMapper;
import com.ruoyi.hqfinance.service.HqNetworkBillDetailService;
import com.ruoyi.logistic.domain.PageDate;
import com.ruoyi.statisticalCenter.domain.vo.ExcelScFlowReportDispositionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HqNetworkBillDetailServiceImpl implements HqNetworkBillDetailService {
    @Autowired
    private HqNetworkBillDetailMapper hqNetworkBillDetailMapper;
    @Autowired
    private HqNetworkDetailCollectMapper hqNetworkDetailCollectMapper;
    @Override
    public List<HqNetworkBillDetail> selectAll(PageDate pageDate) {
        PageHelper.startPage(pageDate);
        return hqNetworkBillDetailMapper.selectAll();
    }

    @Override
    public Boolean insert(HqNetworkBillDetail hqNetworkBillDetail) {
        return hqNetworkBillDetailMapper.insert(hqNetworkBillDetail);
    }

    @Override
    public Boolean update(HqNetworkBillDetail hqNetworkBillDetail) {
        return hqNetworkBillDetailMapper.update(hqNetworkBillDetail);
    }

    @Override
    public Boolean deleteById(Integer id) {
        return hqNetworkBillDetailMapper.deleteById(id);
    }

    @Override
    public List<HqNetworkBillDetail> search(HqNetworkBillDetailVo hqNetworkBillDetailVo) {
        PageHelper.startPage(hqNetworkBillDetailVo.getPageDate());
        return hqNetworkBillDetailMapper.search(hqNetworkBillDetailVo);
    }

    @Override
    public HqNetworkDetailCollect collectAmount(HqNetworkBillDetailVo hqNetworkBillDetailVo) {
        return hqNetworkDetailCollectMapper.collectAmount(hqNetworkBillDetailVo);
    }

    @Override
    @Async
    public R exportAsync(Integer pageNum, Integer pageSize, String path, String sheetName) {
        // 获取代码执行前的时间戳
        long startTime = System.currentTimeMillis();
        sheetName = sheetName==null || sheetName.equals("")?"sheet1":sheetName;
        if ((pageNum!=null && pageNum !=0 ) || (pageSize!=null && pageSize!=0)){
            PageDate pageDate = new PageDate();
            pageDate.setPageNum(pageNum!=null && pageNum !=0?pageNum:pageDate.getPageNum());
            pageDate.setPageSize(pageSize!=null&&pageSize!=0?pageSize:pageDate.getPageSize());
            PageHelper.startPage(pageDate);
        }

        List<HqNetworkBillDetail> lise = hqNetworkBillDetailMapper.selectAll();
        System.out.println("查询数据的耗时:"+(System.currentTimeMillis()-startTime));
        EasyExcel.write(path, HqNetworkBillDetail.class).sheet(sheetName).doWrite(lise);
        // 获取代码执行后的时间戳
        long endTime = System.currentTimeMillis();

        // 计算代码执行所需的毫秒数
        long executionTime = endTime - startTime;

        System.out.println("执行这段代码的耗时(毫秒)："+executionTime);
        return R.ok();
    }

}
